using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Firestarter.DB.Schema.Logic;

namespace Firestarter.DB.Schema.Test.Logic
{
    public class DBSchemaTemplateForTests : DBSchemaTemplate
    {
        public const string DefaultCatalogName = "TEST_CATALOG";
        public const string DefaultSchemaName = "TEST_SCHEMA";
        public const string DefaultTableType = "BASE TABLE";

        // Databases

        public void CreateDatabaseDataRow(DataTable dataTable, string catalogName, string schemaName)
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow[Naming.CatalogName] = catalogName;
            dataRow[Naming.SchemaName] = schemaName;
            dataTable.Rows.Add(dataRow);
        }

        // Data Types

        public void CreateDataTypeDataRow(DataTable dataTable)
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow[Naming.TypeName] = "";
            dataRow[Naming.ProviderDbType] = "";
            dataRow[Naming.ColumnSize] = "";
            dataRow[Naming.CreateFormat] = "";
            dataRow[Naming.CreateParameters] = "";
            dataRow[Naming.SystemType] = "";
            dataRow[Naming.IsAutoIncrementable] = "";
            dataRow[Naming.IsBestMatch] = "";
            dataRow[Naming.IsCaseSensitive] = "";
            dataRow[Naming.IsFixedLength] = "";
            dataRow[Naming.IsFixedPrecisionScale] = "";
            dataRow[Naming.IsLong] = "";
            dataRow[Naming.IsNullable] = "";
            dataRow[Naming.IsSearchable] = "";
            dataRow[Naming.IsSearchableWithLike] = "";
            dataRow[Naming.IsUnsigned] = "";
            dataRow[Naming.MaximumScale] = "";
            dataRow[Naming.MinimumScale] = "";
            dataRow[Naming.IsConcurrencyType] = "";
            dataRow[Naming.IsLiteralSupported] = "";
            dataRow[Naming.LiteralPrefix] = "";
            dataRow[Naming.LiteralSuffix] = "";

            dataTable.Rows.Add(dataRow);
        }

        // Tables

        public void CreateTableDataRow(DataTable dataTable, string tableName)
        {
            CreateTableDataRow(dataTable, tableName, DefaultTableType);
        }

        public void CreateTableDataRow(DataTable dataTable, string tableName, string tableType)
        {
            CreateTableDataRow(dataTable, DefaultCatalogName, DefaultSchemaName, tableName, tableType);
        }

        public void CreateTableDataRow(DataTable dataTable, string tableCatalog, string tableSchema, string tableName, string tableType)
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow[Naming.TableCatalog] = tableCatalog;
            dataRow[Naming.TableSchema] = tableSchema;
            dataRow[Naming.TableName] = tableName;
            dataRow[Naming.TableType] = tableType;
            dataTable.Rows.Add(dataRow);
        }

        // Columns

        public void CreateColumnDataRow(DataTable dataTable, string columnName)
        {
            CreateColumnDataRow(dataTable, DefaultCatalogName, DefaultSchemaName, null, columnName);
        }

        public void CreateColumnDataRow(DataTable dataTable, string tableName, string columnName)
        {
            CreateColumnDataRow(dataTable, DefaultCatalogName, DefaultSchemaName, tableName, columnName);
        }

        public void CreateColumnDataRow(DataTable dataTable, string columnName, string columnType, int maxCharLength)
        {
            CreateColumnDataRow(dataTable, DefaultCatalogName, DefaultSchemaName, null, columnName, columnType, maxCharLength);
        }

        public void CreateColumnDataRow(DataTable dataTable, string tableCatalog, string tableSchema,
                                         string tableName, string columnName)
        {
            CreateColumnDataRow(dataTable, tableCatalog, tableSchema, tableName, columnName, "CHAR", 38);
        }

        public void CreateColumnDataRow(DataTable dataTable, string tableCatalog, string tableSchema, string tableName, string columnName, string columnType, int maxCharLength)
        {
            CreateColumnDataRow(dataTable, tableCatalog, tableSchema, tableName, columnName, 1,
                                 "default column value", true, columnType, maxCharLength, 0, 0, false);
        }

        public void CreateColumnDataRow(DataTable dataTable, string tableCatalog, string tableSchema,
            string tableName, string columnName, int ordinalPosition, string defaultValue, 
            bool isNullable, string columnType, int maxCharLength, 
            int numericPrecision, int numericScale, bool autoIncrement)
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow[Naming.TableCatalog] = tableCatalog;
            dataRow[Naming.TableSchema] = tableSchema;
            dataRow[Naming.TableName] = tableName;
            dataRow[Naming.ColumnName] = columnName;
            dataRow[Naming.OrdinalPosition] = ordinalPosition;
            dataRow[Naming.ColumnDefault] = defaultValue;
            dataRow[Naming.ColumnIsNullable] = isNullable;
            dataRow[Naming.DataType] = columnType;
            dataRow[Naming.CharacterMaximumLength] = maxCharLength;
            dataRow[Naming.NumericPrecision] = numericPrecision;
            dataRow[Naming.NumericScale] = numericScale;
            dataRow[Naming.ColumnIsAutoIncrement] = autoIncrement;
            dataTable.Rows.Add(dataRow);
        }

        // Keys

        public void CreateUniqueKeyColumnDataRow(DataTable dataTable, string keyName, string columnName)
        {
            CreateUniqueKeyColumnDataRow(dataTable, null, keyName, columnName);
        }
        public void CreateUniqueKeyColumnDataRow(DataTable dataTable, string tableName, string keyName, string columnName)
        {
            CreateUniqueKeyColumnDataRow(dataTable, DefaultCatalogName, DefaultSchemaName,
                                         tableName, DefaultCatalogName, DefaultSchemaName, keyName, columnName, 1);
        }

        public void CreateUniqueKeyColumnDataRow(DataTable dataTable,
            string tableCatalog, string tableSchema, string tableName,
            string keyCatalog, string keySchema, string keyName,
            string columnName, int ordinalPosition)
        {
            CreateKeyDataRow(dataTable, tableCatalog, tableSchema, tableName, 
                keyCatalog, keySchema, keyName, columnName, ordinalPosition);
        }

        private void CreateKeyDataRow(DataTable dataTable, 
            string tableCatalog, string tableSchema, string tableName, 
            string keyCatalog, string keySchema, string keyName, 
            string columnName, int ordinalPosition)
        {
            DataRow dataRow = dataTable.NewRow(); 
            dataRow[Naming.TableCatalog] = tableCatalog ;
            dataRow[Naming.TableSchema] = tableSchema;
            dataRow[Naming.TableName] = tableName;
            dataRow[Naming.KeyCatalog] = keyCatalog;
            dataRow[Naming.KeySchema] = keySchema;
            dataRow[Naming.KeyName] = keyName;
            dataRow[Naming.ColumnName] = columnName;
            dataRow[Naming.OrdinalPosition] = ordinalPosition;
            dataTable.Rows.Add(dataRow);
        }

        public void CreatePrimaryKeyColumnDataRow(DataTable dataTable, string columnName)
        {
            CreatePrimaryKeyColumnDataRow(dataTable, null, columnName);
        }

        public void CreatePrimaryKeyColumnDataRow(DataTable dataTable, string tableName, string columnName)
        {
            CreatePrimaryKeyColumnDataRow(dataTable, DefaultCatalogName, DefaultSchemaName, tableName, 
                null,null,null,columnName, 1);
        }

        public void CreatePrimaryKeyColumnDataRow(DataTable dataTable, 
            string tableCatalog, string tableSchema, string tableName, 
            string keyCatalog, string keySchema, string keyName, 
            string columnName, int ordinalPosition)
        {
            CreateKeyDataRow(dataTable, tableCatalog, tableSchema, tableName,
                keyCatalog, keySchema, keyName, columnName, ordinalPosition);
        }

        // Foreign Keys

        public void CreateForeignKeyColumnDataRow(DataTable dataTable, 
            string foreignKeyName, string foreignKeyTableName, string foreignKeyColumnName, 
            string referencedTableName, string referencedColumnName, int ordinalPosition)
        {
            CreateForeignKeyColumnDataRow(dataTable, DefaultCatalogName, DefaultSchemaName, foreignKeyTableName,
                                          DefaultCatalogName, DefaultSchemaName, foreignKeyName, foreignKeyColumnName,
                                          DefaultCatalogName, DefaultSchemaName, referencedTableName,
                                          referencedColumnName, ordinalPosition);
        }

        public void CreateForeignKeyColumnDataRow(DataTable dataTable, 
            string tableCatalog, string tableSchema, string tableName, 
            string keyCatalog, string keySchema, string keyName, string columnName,
            string referencedTableCatalog, string referencedTableSchema,
            string referencedTableName, string referencedColumnName,
            int ordinalPosition)
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow[Naming.TableCatalog] = tableCatalog;
            dataRow[Naming.TableSchema] = tableSchema;
            dataRow[Naming.TableName] = tableName;
            dataRow[Naming.KeyCatalog] = keyCatalog;
            dataRow[Naming.KeySchema] = keySchema;
            dataRow[Naming.KeyName] = keyName;
            dataRow[Naming.ColumnName] = columnName;
            dataRow[Naming.ReferencedTableCatalog] = referencedTableCatalog;
            dataRow[Naming.ReferencedTableSchema] = referencedTableSchema;
            dataRow[Naming.ReferencedTableName] = referencedTableName;
            dataRow[Naming.ReferencedColumnName] = referencedColumnName;
            dataRow[Naming.OrdinalPosition] = ordinalPosition;
            dataTable.Rows.Add(dataRow);
        }

        public void CreateForeignKeyRowsWithTwoColumns(DataTable foreignKeyList, string foreignkeyName, string foreignKeyTableName, string foreignKeyColumnName, string referencedTableName, string referencedColumnName, string foreignKeyColumnName2, string referencedColumnName2)
        {
            CreateForeignKeyColumnDataRow(foreignKeyList, foreignkeyName, foreignKeyTableName, foreignKeyColumnName, referencedTableName, referencedColumnName, 1);
            CreateForeignKeyColumnDataRow(foreignKeyList, foreignkeyName, foreignKeyTableName, foreignKeyColumnName2, referencedTableName, referencedColumnName2, 2);
        }

        public DataTable CreateDataTableWithTwoColumn(string tableName, string columnName, string columnName2)
        {
            DataTable dataTable = CreateColumnsDataTable();
            CreateColumnDataRow(dataTable, columnName);
            CreateColumnDataRow(dataTable, columnName2);
            return dataTable;
        }

        public DataTable CreateDataTableWithOneColumn(string tableName,string columnName)
        {
            DataTable dataTable = CreateColumnsDataTable();
            CreateColumnDataRow(dataTable, tableName, columnName);
            return dataTable;
        }

        public DataTable CreateTableDataTable_OneTable(string untouchedTableName)
        {
            DataTable updatedTableList = CreateColumnsDataTable();
            CreateTableDataRow(updatedTableList, untouchedTableName);
            return updatedTableList;
        }


        
    }
}
